<!--职位管理-->
<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item class="form-button-right">
				<el-button v-auth="'module:role:save'" type="primary" @click="addOrUpdateHandle()">新增职位</el-button>
				<el-upload :action="''" :before-upload="beforeUpload" :on-success="handleSuccess" :show-file-list="false">
					<el-button type="info">导入</el-button>
				</el-upload>
				<el-button type="danger" @click="toggleExpandAll()">
					<el-icon style="width: 100%"> <Sort />更多操作</el-icon>
				</el-button>
			</el-form-item>

			<el-form-item>
				<KeyQuery v-model="state.queryForm.keyword" @getDataList="getDataList()" />
			</el-form-item>
		</el-form>

		<el-row>
			<el-col :span="4">
				<el-table
					v-if="refreshTable"
					v-loading="state.dataListLoading"
					:default-expand-all="true"
					:data="dataListLeft.dataList"
					row-key="id"
					:cell-style="{ borderColor: '#00000000' }"
					:header-cell-style="{ borderColor: '#00000000' }"
				>
					<el-table-column prop="name" label="职位类别" header-align="left"></el-table-column>
				</el-table>
			</el-col>

			<el-col :span="20">
				<el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%" @selection-change="selectionChangeHandle">
					<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
					<el-table-column prop="id" label="主键" header-align="center" align="center" v-if="false"></el-table-column>

					<el-table-column prop="name" label="职位名称" header-align="center" align="center"></el-table-column>
					<el-table-column prop="code" label="职位编码" header-align="center" align="center"></el-table-column>
					<el-table-column prop="organName" label="所属组织" header-align="center" align="center"></el-table-column>
					<el-table-column prop="jobName" label="对应职务" header-align="center" align="center"></el-table-column>

					<el-table-column prop="jobLevelName" label="对应职级" header-align="center" align="center"></el-table-column>
					<el-table-column prop="isEnable" label="是否启用" header-align="center" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.isEnable === 0" type="info">禁用</el-tag>
							<el-tag v-if="scope.row.isEnable === 1" type="success">启用</el-tag>
						</template>
					</el-table-column>

					<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
						<template #default="scope">
							<el-button v-auth="'module:position:update'" type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
							<el-button v-auth="'module:position:delete'" type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination
					:current-page="state.page"
					:page-sizes="state.pageSizes"
					:page-size="state.limit"
					:total="state.total"
					layout="total, sizes, prev, pager, next, jumper"
					@size-change="sizeChangeHandle"
					@current-change="currentChangeHandle"
				>
				</el-pagination>
			</el-col>
		</el-row>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList" width="500px"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="ModulePositionIndex">
import { useCrud } from '@/hooks'
import { reactive, ref, nextTick } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { ElMessage, UploadProps } from 'element-plus'
import service from '@/utils/request'

import AddOrUpdate from './plane/position-a-u.vue'

const state: IHooksOptions = reactive({
	dataListUrl: '/nb/position/page',
	deleteUrl: '/nb/position/delete',
	queryForm: {}
})

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id)
}

// 是否重新渲染表格状态
const refreshTable = ref(true)

const dataListLeft = {
	dataList: []
}

const findListLeft = () => {
	service
		.get('/nb/position_type/list', {
			params: {}
		})
		.then((res: any) => {
			dataListLeft.dataList = res.data
		})
}
findListLeft()

// 是否展开，默认全部折叠
const isExpandAll = ref(true)
/**
 * 切换 展开/折叠
 */
const toggleExpandAll = () => {
	refreshTable.value = false
	isExpandAll.value = !isExpandAll.value
	nextTick(() => {
		refreshTable.value = true
	})
}

//导入导出
const handleSuccess: UploadProps['onSuccess'] = (res, file) => {
	if (res.code !== 0) {
		ElMessage.error('上传失败：' + res.msg)
		return false
	}

	ElMessage.success({
		message: '上传成功',
		duration: 500,
		onClose: () => {
			getDataList()
		}
	})
}

const beforeUpload: UploadProps['beforeUpload'] = file => {
	if (file.size / 1024 / 1024 / 1024 / 1024 > 1) {
		ElMessage.error('文件大小不能超过100M')

		return false
	}
	return true
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
</script>
